package com.bocloud.cmp.service.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.resource.CloudVendor;
import com.bocloud.cmp.model.Butler;
import com.bocloud.common.enums.BoCloudConfig;
import com.bocloud.coordinator.config.client.SimpleZkClient;
import com.bocloud.coordinator.config.client.ZkClientHelper;
import com.bocloud.coordinator.esb.intf.EsbService;

/**
 * 注册云供应商信息到ZK
 * 
 * @author dongkai
 *
 */
public class RegisterVendor {

	private static Logger logger = LoggerFactory.getLogger(RegisterVendor.class);

	public static void register(EsbService esbService, ZkClientHelper zkClientHelper, List<CloudVendor> list) {
		try {
			Map<String, String> map = new HashMap<>();
			Butler butler = null;
			for (CloudVendor cloudVendor : list) {
				butler = new Butler(cloudVendor.getAccount(), cloudVendor.getPassword(), cloudVendor.getAccessKey(),
						cloudVendor.getSecretKey(), "", cloudVendor.getTenant(), cloudVendor.getAddress(),
						cloudVendor.getType(), cloudVendor.getDomainName(), cloudVendor.getProjectName(),
						cloudVendor.getVersion());
				map.put(cloudVendor.getUuid(), butler.toString());
			}
			String path = BoCloudConfig.Butler.getZkPath();
			SimpleZkClient client = new SimpleZkClient(zkClientHelper.getClient());
			if (!client.isExists(path, false)) {
				client.create(path, "");
			}
			client.setData(path, JSONObject.parseObject(JSONObject.toJSONString(map)).toString());
		} catch (Exception e) {
			logger.error("register vendor failure:", e);
		}
	}

}
